import apiClient from '@/api/axios.js';

export class SecondHandItemApi {

    // 新增二手物品
    static async createSecondHandItem(params) {
        const { sellerID, title, description, price, creationTime, status } = params;
        try {
            const response = await apiClient.post('/secondhanditem', {
                sellerID,
                title,
                description,
                price,
                creationTime,
                status
            });
            return response;
        } catch (error) {
            throw new Error(error.message);
        }
    }

    // 更新二手物品
    static async updateSecondHandItem(itemID, params) {
        const { sellerID, title, description, price, creationTime, status } = params;
        try {
            // 构建完整的更新对象，包含itemID
            const updateData = {
                itemID: Number(itemID),  // 确保itemID是数字类型
                sellerID,
                title,
                description,
                price,
                creationTime,
                status
            };
            
            // 使用正确的URL（不包含ID）
            const response = await apiClient.put('/secondhanditem', updateData);
            return response;
        } catch (error) {
            throw new Error(error.message);
        }
    }

    // 更新二手物品状态（专门用于确认收货和取消订单）
    static async updateItemStatus(itemID, status) {
        try {
            // 先获取当前商品信息
            const currentItem = await this.getSecondHandItemById(itemID);
            
            if (!currentItem || !currentItem.data) {
                throw new Error('商品不存在');
            }
            
            const itemData = currentItem.data;
            
            // 构建完整的更新对象，包含itemID
            const updateData = {
                itemID: Number(itemID),  // 确保itemID是数字类型
                sellerID: itemData.sellerID,
                title: itemData.title,
                description: itemData.description,
                price: itemData.price,
                creationTime: itemData.creationTime,
                status: status
            };
            
            console.log('更新商品状态请求数据:', updateData);
            
            // 使用正确的URL（不包含ID）
            const response = await apiClient.put('/secondhanditem', updateData);
            
            console.log(`更新商品 ${itemID} 状态为 ${status}, 响应:`, response);
            return response;
        } catch (error) {
            console.error(`更新商品状态失败:`, error);
            throw new Error(error.message || '更新商品状态失败');
        }
    }

    // 删除二手物品
    static async deleteSecondHandItem(itemID) {
        try {
            const response = await apiClient.delete(`/secondhanditem/${itemID}`);
            return response;
        } catch (error) {
            throw new Error(error.message);
        }
    }

    // 获取单个二手物品
    static async getSecondHandItemById(itemID) {
        try {
            const response = await apiClient.get(`/secondhanditem/${itemID}`);
            return response;
        } catch (error) {
            throw new Error(error.message);
        }
    }

    // 查询所有二手物品
    static async getAllSecondHandItems() {
        try {
            const response = await apiClient.get('/secondhanditem');
            return response;
        } catch (error) {
            throw new Error(error.message);
        }
    }

    // 根据sellerID查询二手物品
    static async getSecondHandItemsBySellerId(sellerID) {
        try {
            const response = await apiClient.get(`/secondhanditem/seller/${sellerID}`);
            return response;
        } catch (error) {
            throw new Error(error.message);
        }
    }
}
